(* sprintf.sml --- 
 * 
 * Filename: sprintf.sml
 * Description: 
 * Author: Zhong Zhuang
 * Maintainer: 
 * Created: 05 15 2010 13:52:46 (+0800)
 * Version: 
 * Last-Updated: 05 15 2010 13:53:02 (+0800)
 *           By: Zhong Zhuang
 *     Update #: 1
 * URL: 
 * Keywords: 
 * Compatibility: 
 * 
 *)

(* Commentary: 
 * 
 * 
 * 
 *)

(* Change log:
 * 
 * 
 *)

(*             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 *                     Version 2, December 2004
 * 
 *  Copyright (C) 2004 Sam Hocevar
 *   14 rue de Plaisance, 75014 Paris, France
 *  Everyone is permitted to copy and distribute verbatim or modified
 *  copies of this license document, and changing it is allowed as long
 *  as the name is changed.
 * 
 *             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 *    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 * 
 *   0. You just DO WHAT THE FUCK YOU WANT TO.

 *)

(* Code: *)

fun $ (_, f) = f (fn p => p ()) ignore
fun fprintf out f = f (out, fn x=>x)

fun write(out,s)= let val _ = out:=(!out)^s
		  in 
		      (!out)
		  end

val out = ref ""
val sprintf = fn z => let val _ = out:="" in fprintf out z end

fun one ((out, f), make) g =
    g (out, fn r =>
               f (fn p =>
		     make (fn s => 
			      r (fn () => (p (); write(out,s))))))

fun ` x s = one (x, fn f => f s)
fun spec to x = one (x, fn f => f o to)

val I = fn z => spec Int.toString z
val R = fn z => spec Real.toString z
val S = fn z => spec String.toString z 

val s = sprintf `"Int:"I`" Real:"R`"\n" $ 1 2.0


(* sprintf.sml ends here *)
